#include "valuewidget.h"

ValueWidget::ValueWidget( QList< QMap<QString, QString> > l, QString s )
{
this->setAttribute(Qt::WA_DeleteOnClose);
setupUi(this);
readSettings();

label->setText(s);
tableWidget->setColumnWidth(0, 250);

QListIterator< QMap<QString, QString> > l_it(l);
    for ( int i = 0; i < l.size(); ++i )
    {
    tableWidget->insertRow(i);
    QMap<QString, QString> m = l.at(i);
    QString curr = m.value("curr");
    QString value_str = m.value("value");
    QTableWidgetItem * curr_i = new QTableWidgetItem(curr);
    QTableWidgetItem * value_i = new QTableWidgetItem(value_str);
    tableWidget->setItem( i, 0, value_i);
    tableWidget->setItem( i, 1, curr_i);
    }
}

void ValueWidget::writeSettings()
{
QSettings settings( QSettings::IniFormat,
                    QSettings::UserScope,
                    QApplication::organizationName(),
                    QApplication::applicationName() );
settings.setFallbacksEnabled(false);
settings.beginGroup( QLatin1String("ValueWidget") );
settings.setValue( QLatin1String("size"), QVariant( this->size() ) );
settings.setValue( QLatin1String("pos"), QVariant( this->pos() ) );
settings.endGroup();
}

void ValueWidget::readSettings()
{
QSettings settings( QSettings::IniFormat,
                    QSettings::UserScope,
                    QApplication::organizationName(),
                    QApplication::applicationName() );
settings.setFallbacksEnabled(false);                
settings.beginGroup( QLatin1String("ValueWidget") );
resize( settings.value( QLatin1String("size"), QSize( 442, 380 ) ).toSize() );
move( settings.value( QLatin1String("pos"), QPoint( 0, 0 ) ).toPoint() );
settings.endGroup();
}

void ValueWidget::closeEvent( QCloseEvent * e )
{
writeSettings();
e->accept();
}
